package org.example.demo21.service.impl;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.demo21.chain.ChainFilter;
import org.example.demo21.chain.impl.AgeFilter;
import org.example.demo21.chain.impl.NameFilter;
import org.example.demo21.chain.impl.SexFilter;
import org.example.demo21.decorator.Coffee;
import org.example.demo21.decorator.coffee.Black;
import org.example.demo21.decorator.coffee.Coconut;
import org.example.demo21.decorator.coffee.Latte;
import org.example.demo21.entity.bo.DemoBO;
import org.example.demo21.iterator.DrinkIterator;
import org.example.demo21.iterator.entity.MenuItem;
import org.example.demo21.iterator.impl.CoffeeMenu;
import org.example.demo21.iterator.impl.DrinkShopMenu;
import org.example.demo21.service.DesignModeService;
import org.example.demo21.strategy.AgeStrategy;
import org.example.demo21.strategy.context.AgeContext;
import org.example.demo21.temp.person.LuckyGuy;
import org.example.demo21.temp.person.UnluckyGuy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * @author guocong
 * @since 2024/3/29 13:44
 */
@Slf4j
@Service
public class DesignModeServiceImpl implements DesignModeService {

    @Resource
    private DrinkShopMenu shopMenu;
    @Resource
    private CoffeeMenu coffeeMenu;

    @Resource
    private AgeContext ageContext;


    @SneakyThrows
    @Override
    public void temp() {
        Thread.sleep(1000);

        var a = new UnluckyGuy();
        var b = new LuckyGuy();

        a.myDay();
        System.out.println("\n\n");
        Thread.sleep(1000);
        b.myDay();
    }

    @Override
    public void chain(DemoBO demoBO) {
        ChainFilter chainFilter = new ChainFilter();
        // 责任链里边的检查环节
        var ageFilter = new AgeFilter();
        var nameFilter = new NameFilter();
        var sexFilter = new SexFilter();
        // 构建责任链  - 中途出现检查未通过的，则不继续执行
        chainFilter.add(ageFilter).add(nameFilter).add(sexFilter);
        boolean b = chainFilter.doFilter(demoBO);
        System.out.println(b);
    }

    @Override
    public DemoBO strategy(DemoBO demoBO) {
        try {
            AgeStrategy ageStrategy = ageContext.getStrategy(demoBO.getStrategy());
            // 根据传入策略，处理年龄分段问题
            ageStrategy.ageProcess(demoBO);
        } catch (Exception e) {
            log.error("error 日志输出，{}", "出异常了！");
        }
        return demoBO;
    }

    @Override
    public void decorator() {
        Coffee coffee = new Black();
        int color = 31;
        System.out.println("\033[" + color + "m描述：" + coffee.desc() + "\033[0m");
        System.out.println("\033[" + color + "m价格：" + coffee.price() + "\033[0m");
        color++;
        coffee = new Latte(coffee);
        System.out.println("\033[" + color + "m描述：" + coffee.desc() + "\033[0m");
        System.out.println("\033[" + color + "m价格：" + coffee.price() + "\033[0m");
        color++;
        coffee = new Coconut(coffee);
        System.out.println("\033[" + color + "m描述：" + coffee.desc() + "\033[0m");
        System.out.println("\033[" + color + "m价格：" + coffee.price() + "\033[0m");

    }

    @Override
    public void iter() {
        DrinkIterator shopIter = shopMenu.createIter();
        DrinkIterator coffeeIter = coffeeMenu.createIter();
        System.out.println("\033[31m 咖啡品牌 \033[0m");
        printMenu(shopIter);
        System.out.println("\033[32m 咖啡种类 \033[0m");
        printMenu(coffeeIter);

    }

    private void printMenu(DrinkIterator iterator) {
        while (iterator.hasNext()) {
            MenuItem menuItem = (MenuItem) iterator.next();
            System.out.print("code : " + menuItem.getCode() + "\n");
            System.out.print("name : " + menuItem.getName() + "\n");
            System.out.println("description : " + menuItem.getDescription() + "\n");
        }
    }

}
